package primeiraConexao;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class Conexao {
private Connection conn;
    
    /** Creates a new instance of Conexao */
    public Conexao() {
        try {
            Properties param = new Properties();
            param.load(Conexao.class.getResourceAsStream("banco.properties"));
            String driver = "com.mysql.jdbc.Driver";
            String url = param.getProperty("url");
            String login ="root" ;
            String senha = "102660";
            Class.forName(driver);
            conn = DriverManager.getConnection(url, login, senha);
        } catch (IOException e) {
            System.out.println("ClassNotFoundException: " + e.getMessage());
        } catch (ClassNotFoundException e) {
            System.out.println("ClassNotFoundException: " + e.getMessage());
        } catch (SQLException e) {
            System.out.println("SQLException: " + e.getMessage());
        }
    }
    
    public Connection getConexao() {
        return conn;
    }
    
    public void fechar(ResultSet rs)  {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                System.out.println("SQLException: " + e.getMessage());
            }
        }
    }
    
    public void fechar(Statement stmt) {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                System.out.println("SQLException: " + e.getMessage());
            }
        }        
    }
    
    public void fecharConexao() {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                System.out.println("SQLException: " + e.getMessage());
            }
        }                
    }

}
